package com.changgou.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.common.pojo.HttpClient;
import com.changgou.pay.service.WXPayService;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WXPayServiceImpl implements WXPayService {
  // 微信支付的账号ID
  @Value("${weixin.appid}")
  private String appid;
  // 微信支付的商户号
  @Value("${weixin.partner}")
  private String partner;
  // 微信支付的秘钥
  @Value("${weixin.partnerkey}")
  private String partnerkey;
  // 微信支付的通知地址
  @Value("${weixin.notifyuri}")
  private String notifyuri;

  // 统一下单接口调用
  @Override
  public Map nativePay(Map<String, String> parampMap)  {
    try {
      // 添加参数
      Map<String, String> payMap = new HashMap<>();
      createPay(parampMap, payMap);
      // 把map转换成xml
      String resultxml = WXPayUtil.generateSignedXml(payMap, partner);
      // url地址
      String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
      HttpClient httpClient = new HttpClient(url);
      // 提交的方式
      httpClient.setHttps(true);
      httpClient.setXmlParam(resultxml);
      // 执行请求
      httpClient.post();
      // 获取返回的结果
      String result = httpClient.getContent();
      // 转换成map
      Map<String, String> resultMap = WXPayUtil.xmlToMap(result);
      return resultMap;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return  null;
  }

  private void createPay(Map<String, String> parampMap, Map<String, String> payMap) {
    // 账号
    payMap.put("appid", appid);
    // 商户号
    payMap.put("mch_id", partner);
    // 随机字符串
    payMap.put("nonce_str", WXPayUtil.generateNonceStr());
    // 商品描述
    payMap.put("body", "商品便宜");
    // 订单号
    payMap.put("out_trade_no", parampMap.get("outtradeno"));
    // 标价金额
    payMap.put("total_fee", parampMap.get("totalfee"));
    // 终端ip
    payMap.put("spbill_create_ip", "127.0.0.1");
    // 微信交易的通知地址
    payMap.put("notify_url", notifyuri);
    // 交易类型
    payMap.put("trade_type", "NATIVE");

    // 获得自定义的数据
    String exchange=parampMap.get("exchange");
    String routingkey=parampMap.get("routingkey");
    Map<String,String> map = new HashMap<>();
    map.put("exchange",exchange);
    map.put("routingkey",routingkey);
    String attach = JSON.toJSONString(map);
    payMap.put("attach",attach);
  }


  // 统一下单接口调用
  @Override
  public Map queryState (String outtrandno)  {
    try {
      // 添加参数
      Map<String, String> payMap = new HashMap<>();
      // 账号
      payMap.put("appid", appid);
      // 商户号
      payMap.put("mch_id", partner);
      // 随机字符串
      payMap.put("nonce_str", WXPayUtil.generateNonceStr());
      // 订单号
      payMap.put("out_trade_no", outtrandno);
      // 把map转换成xml
      String resultxml = WXPayUtil.generateSignedXml(payMap, partner);
      // url地址
      String url = "https://api.mch.weixin.qq.com/pay/orderquery";
      HttpClient httpClient = new HttpClient(url);
      // 提交的方式
      httpClient.setHttps(true);
      httpClient.setXmlParam(resultxml);
      // 执行请求
      httpClient.post();
      // 获取返回的结果
      String result = httpClient.getContent();
      // 转换成map
      Map<String, String> resultMap = WXPayUtil.xmlToMap(result);
      return resultMap;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return  null;
  }

}
